package com.sxt.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.bus.domain.Goods;
import com.sxt.bus.domain.Goodstype;
import com.sxt.bus.domain.Provider;
import com.sxt.bus.domain.Sales;
import com.sxt.bus.service.GoodsService;
import com.sxt.bus.service.GoodstypeService;
import com.sxt.bus.service.ProviderService;
import com.sxt.bus.service.SalesService;
import com.sxt.bus.vo.SalesVo;
import com.sxt.opd.domain.Patient;
import com.sxt.opd.service.PatientService;
import com.sxt.sys.common.Constast;
import com.sxt.sys.common.DataGridView;
import com.sxt.sys.common.ResultObj;
import com.sxt.sys.common.WebUtils;
import com.sxt.sys.domain.User;
import com.sxt.sys.vo.LoginfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2019-12-26
 */
@RestController
@RequestMapping("/sales")
public class SalesController {

    @Autowired
    private SalesService salesService;

    @Autowired
    private PatientService patientService;
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ProviderService providerService;

    @Autowired
    private GoodstypeService goodstypeService;

    /**
     * 查询
     */
    @RequestMapping("loadAllSales")
    public DataGridView loadAllSales(SalesVo salesVo) {
        IPage<Sales> page = new Page<>(salesVo.getPage(), salesVo.getLimit());
        QueryWrapper<Sales> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(salesVo.getPatientid()!=null&&salesVo.getPatientid()!=0,"patientid",salesVo.getPatientid());
        queryWrapper.eq(salesVo.getGoodsid()!=null&&salesVo.getGoodsid()!=0,"goodsid",salesVo.getGoodsid());
        queryWrapper.eq(salesVo.getIspay()!=null&&salesVo.getIspay()!=0,"ispay",salesVo.getIspay());
        List<Integer> listgoodsid = new ArrayList<Integer>();
        if(null!=salesVo.getProviderid()){
            QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
            goodsQueryWrapper.eq("providerid",salesVo.getProviderid());
            List<Goods> list = goodsService.list(goodsQueryWrapper);
            for (Goods goods : list) {
                listgoodsid.add(goods.getId());
            }
        }
        queryWrapper.in(salesVo.getProviderid()!=null && salesVo.getProviderid()!=0,
                "goodsid",listgoodsid);
        queryWrapper.ge(salesVo.getStartTime()!=null, "salestime", salesVo.getStartTime());
        queryWrapper.le(salesVo.getEndTime()!=null, "salestime", salesVo.getEndTime());
        queryWrapper.like(StringUtils.isNotBlank(salesVo.getGoodslistnum()), "goodslistnum", salesVo.getGoodslistnum());
        queryWrapper.like(StringUtils.isNotBlank(salesVo.getRemark()), "remark", salesVo.getRemark());
        queryWrapper.like(StringUtils.isNotBlank(salesVo.getOutperson()), "outperson", salesVo.getOutperson());
        queryWrapper.orderByDesc("salestime");
        this.salesService.page(page, queryWrapper);
        List<Sales> records = page.getRecords();
        for (Sales sales : records) {
            Patient patient = this.patientService.getById(sales.getPatientid());
            if(null!=patient) {
                sales.setPatientname(patient.getPatientname());
            }
            Goods goods = this.goodsService.getById(sales.getGoodsid());
            if(null!=goods) {
                sales.setGoods(goods);
                Provider  provider = this.providerService.getById(goods.getProviderid());
                if(null!=provider){
                    sales.setProvidername(provider.getProvidername());
                    sales.setProviderid(provider.getId());
                }
            }
        }
        return new DataGridView(page.getTotal(), records);
    }
    /**
     * 添加
     */
    @RequestMapping("addSales")
    public ResultObj addSales(SalesVo salesVo) {
        //System.out.println(salesVo.getSaleprice()+"=======================================");
        try {
            salesVo.setSalestime(new Date());
            User user = (User) WebUtils.getSession().getAttribute("user");
            salesVo.setOperateperson(user.getName());
            this.salesService.save(salesVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }
    /**
     * 添加
     */
    @RequestMapping("addMenzhenSales")
    public ResultObj addMenzhenSales(SalesVo salesVo) {
        //System.out.println(salesVo.getSaleprice()+"=======================================");
        try {
            salesVo.setSalestime(new Date());
            User user = (User) WebUtils.getSession().getAttribute("user");
            salesVo.setOperateperson(user.getName());
            Goods goods = this.goodsService.getById(salesVo.getGoodsid());
            salesVo.setGoods(goods);
            int num = goods.getOneuse() * goods.getUserate() * salesVo.getDay();
            if (num>goods.getNumber()){
                return ResultObj.ADD_ERRORFORNUM;
            }else {
                if (num == 0) {
                    salesVo.setNumber(1);
                    salesVo.setSumprice(salesVo.getGoods().getPrice());
                } else {
                    salesVo.setNumber(num);
                    salesVo.setSumprice(salesVo.getGoods().getPrice() * num);
                }
                this.salesService.save(salesVo);
                return ResultObj.ADD_SUCCESS;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }
    /**
     * 修改
     */
    @RequestMapping("updateSales")
    public ResultObj updateSales(SalesVo salesVo) {
      //  System.out.println("------------------------------Saleprice:"+salesVo.getSaleprice());
        try {
            this.salesService.updateById(salesVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }
    /**
     * 删除
     */
    @RequestMapping("deleteSales")
    public ResultObj deleteSales(Integer id){
        try {
            this.salesService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
    /**
     * 批量删除
     */
    @RequestMapping("batchDeleteSalesforMenzhen")
    public ResultObj batchDeleteSalesforMenzhen(SalesVo salesVo) {
        try {
            Collection<Serializable> idList=new ArrayList<Serializable>();
            for (Integer id : salesVo.getIds()) {
                idList.add(id);
            }
            this.salesService.removeByIds(idList);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
    /**
     * 批量修改为收费
     */
    @RequestMapping("batchUpdateSalesforMenzhen")
    public ResultObj batchUpdateSalesforMenzhen(SalesVo salesVo) {
        try {
            Integer[] ids = salesVo.getIds();
            for (Integer id : ids) {
                QueryWrapper<Sales> queryWrapper = new QueryWrapper<>();
                salesVo.setId(id);
                salesVo.setIspay(Constast.ISPAY_YES);
                queryWrapper.eq("id",salesVo.getId());
                this.salesService.update(salesVo,queryWrapper);
            }
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
    /**
     * 批量修改为已经出库
     */
    @RequestMapping("batchUpdateSalesIsOut")
    public ResultObj batchUpdateSalesIsOut(SalesVo salesVo) {
        try {
            Integer[] ids = salesVo.getIds();
            for (Integer id : ids) {
                QueryWrapper<Sales> queryWrapper = new QueryWrapper<>();
                User user = (User) WebUtils.getSession().getAttribute("user");
                salesVo.setOutperson(user.getName());
                salesVo.setId(id);
                salesVo.setIsout(Constast.ISOUT);
                queryWrapper.eq("id",salesVo.getId());
                this.salesService.update(salesVo,queryWrapper);
            }
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 查询--门诊药单
     */
    @RequestMapping("loadAllSalesInMenZhen")
    public DataGridView loadAllSalesInMenZhen(SalesVo salesVo) {
        IPage<Sales> page = new Page<>(salesVo.getPage(), salesVo.getLimit());
        QueryWrapper<Sales> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("patientid",salesVo.getPatientid());
        queryWrapper.eq("goodslistnum",salesVo.getGoodslistnum());
       // queryWrapper.eq(salesVo.getPatientid()!=null&&salesVo.getPatientid()!=0,"patientid",salesVo.getPatientid());
        queryWrapper.eq(salesVo.getGoodsid()!=null&&salesVo.getGoodsid()!=0,"goodsid",salesVo.getGoodsid());
        queryWrapper.orderByDesc("salestime");
        this.salesService.page(page, queryWrapper);
        List<Sales> records = page.getRecords();
        for (Sales sales : records) {
            Goods goods = this.goodsService.getById(sales.getGoodsid());
            sales.setGoods(goods);
            Goodstype goodstype = this.goodstypeService.getById(goods.getGoodstypeid());
            sales.setGoodstypename(goodstype.getGoodstypename());
        }
        return new DataGridView(page.getTotal(), records);
    }



}

